

import CanvasManager from '../script/manage/canvasmanager.js'
import symbolDao from '../script/manage/dao/symbolDao.js'
import { createApp } from 'vue'
import APP from './App.vue'
import store from './store/index.js'

import ElementPlus from 'element-plus'
import 'element-plus/theme-chalk/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
const app = createApp(APP);
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}


app.use(store);

if (process.env.NODE_ENV === 'production') {
    console.log = function () { };
    console.warn = function () { };
    console.error = function () { };
    // 重写其他你想要禁用的方法
}

var canvasManager = new CanvasManager.CanvasManager();
window.canvasManager = canvasManager;
window.listenAllKeyEvent = true;
app.use(ElementPlus).mount('#app')

//加载系统模具和图案填充样式参数,异步加载，先要传入回调函数
const onNotifySymbolLoadFinish = function (symbolRet) {
    store.commit("updateSymbolData", symbolRet);
    window.ToolCollapsePanel.value.onNotifySymbolLoadFinish(symbolRet);
}

symbolDao.SymbolDAO.initBaseUnit(onNotifySymbolLoadFinish);

var preKeyDownTime = 0;
var preKeyDownCode = "";

// 监听键盘按下事件
document.addEventListener('keydown', (event) => {
    console.log("keydown***************************************");
    console.log(event);
    if (!window.listenAllKeyEvent){
        return true;
    }
    if ((event.ctrlKey && event.code == "KeyA")
        || (event.ctrlKey && event.code == "KeyZ")
        || event.code == "enter"
        || event.code == "ArrowLeft"
        || event.code == "ArrowRight"
        || event.code == "ArrowUp"
        || event.code == "ArrowDown"
        || event.code == "Escape") {
        // 处理按键
        event.preventDefault();
        // 停止事件传播
        event.stopPropagation();
        onKeyInputFunction(event);
    }
    else {
        onKeyInputFunction(event);
    }
}, false);

var onKeyInputFunction = function (event) {
    const currentDate = new Date();
    if (preKeyDownTime == 0) {
        preKeyDownTime = currentDate.getTime();
        preKeyDownCode = event.code;
    }
    else {
        if (preKeyDownCode == event.code) {
            let curentTime = currentDate.getTime();
            if ((curentTime - preKeyDownTime) < 30) {
                return;
            }
            else {
                preKeyDownTime = currentDate.getTime();
            }
        }
        else {
            preKeyDownTime = currentDate.getTime();
            preKeyDownCode = event.code;
        }
    }
    if (window.mainctl) {
        window.mainctl.onKeyPress(event, null);
    }
    return false;
}

window.keyInputFunction = onKeyInputFunction;

function animateGraph() {
    if (window.mainctl) {
        window.mainctl.animation();
    }
    window.loopHandle = window.requestAnimationFrame(animateGraph);
}

window.loopHandle = 0;
window.animateLoopCalBack = animateGraph;



